<?php 
namespace app\common\controller;//注意修改命名空间
use think\Controller;
use Wechat\Lib\Tools;//微信工具
use Monolog\Logger;//日志类
use Monolog\Handler\StreamHandler;
use alipay\alipaysdkPHP20171201160100\aop\AopClient;//支付宝验签

/**
 * 支付回调类
 * @Author: YagamiZN
 */
class Notify extends Controller{
    /**
     * 微信回调函数
     */
    public function wx_notify(){
        $xml = file_get_contents('php://input');
        //$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        $data=Tools::xml2arr($xml);
        //记录日志
        $log = new Logger('Notify');
        $log->pushHandler(new StreamHandler('log/wxpay/'.date('Y').'/'.date('m-d').'.log', Logger::INFO));
        $log->addInfo('startLog', $data);
        //sign不参与签名算法
        $data_sign = $data['sign'];
        unset($data['sign']);
        $sign = $this->makeSign($data);
        //验证签名并且成功
        if ( ($sign===$data_sign) && ($data['return_code']=='SUCCESS') && ($data['result_code']=='SUCCESS') ) {
            //处理商户订单
            // +----------------------------------------------------------------------
            // | Author: YagamiZN
            // +----------------------------------------------------------------------
        }
        if (true) {
            $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        }else{
            $str='<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
        }
        echo $str;
    }
    /**
     * 支付宝回调函数
     */
    public function ali_notify()
    { // 异步订单结果通知
        $postData=input('post.');
        //记录日志
        $log = new Logger('Notify');
        $log->pushHandler(new StreamHandler('log/alipay/'.date('Y').'/'.date('m-d').'.log', Logger::INFO));
        $log->addInfo('startLog', $postData);
        $alipayNotify = new AopClient();
        $alipayNotify->alipayrsaPublicKey=config('alipay.alipay_public_key');
        //验证签名如果第二个参杂没传就给alipayrsaPublicKey传公钥字符串
        $isOK=$alipayNotify->rsaCheckV1($postData, NULL, "RSA2");
        if ($isOK) {
            if (input('post.trade_status') == 'TRADE_FINISHED' || input('post.trade_status') == 'TRADE_SUCCESS') {
                // 处理支付成功后的逻辑业务
                // +----------------------------------------------------------------------
                // | Author: YagamiZN
                // +----------------------------------------------------------------------
                return 'success';
            }
            return 'fail';
        }
        return 'fail';
    }
    
    /**
     * 微信生成签名
     * @return 签名，本函数不覆盖sign成员变量
     */
    protected function makeSign($data){
        //获取微信支付秘钥
        $key = config('wechat.partnerkey');
        //去空
        $data=array_filter($data);
        //签名步骤一：按字典序排序参数
        ksort($data);
        $string_a=http_build_query($data);
        $string_a=urldecode($string_a);
        //签名步骤二：在string后加入KEY
        $string_sign_temp=$string_a."&key=".$key;
        //签名步骤三：MD5加密
        $sign = md5($string_sign_temp);
        // 签名步骤四：所有字符转为大写
        $result=strtoupper($sign);
        return $result;
    }
}

